

package com.maoshi.shop.security.multishop.service.impl;

import com.maoshi.shop.bean.enums.ShopStatus;
import com.maoshi.shop.bean.model.ShopDetail;
import com.maoshi.shop.dao.ShopDetailMapper;
import com.maoshi.shop.security.comment.exception.UsernameNotFoundException;
import com.maoshi.shop.security.multishop.model.MaoshiShopUser;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户详细信息
 *
 * @author
 */
@Slf4j
@Primary
@Service
@AllArgsConstructor
public class MaoshiShopUserDetailsServiceImpl implements UserDetailsService {

    private final ShopDetailMapper shopDetailMapper;

    /**
     * 用户密码登录
     *
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    @SneakyThrows
    public MaoshiShopUser loadUserByUsername(String username) {
        ShopDetail shopDetail = shopDetailMapper.getShopByMobile(username);
        if (shopDetail == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        MaoshiShopUser shopUser = new MaoshiShopUser();
        shopUser.setEnabled(
                !(Objects.equals(shopDetail.getShopStatus(), ShopStatus.NOTOPEN.value())
                || Objects.equals(shopDetail.getShopStatus(), ShopStatus.OFFLINE.value())));
        shopUser.setPassword(shopDetail.getPassword());
        shopUser.setUserId(shopDetail.getUserId());
        shopUser.setShopId(shopDetail.getShopId());
        shopUser.setUsername(shopDetail.getMobile());
        return shopUser;
    }
}
